package com.rslai.commons.validator.jsonvalidator.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * JSON字符串工具类
 *
 */
public class JsonStringUtil {
    static final Logger LOGGER = LoggerFactory.getLogger(JsonStringUtil.class);

    /**
     * 将json格式字符串转为普通字符串
     * @param jsonStr json字符串
     * @return string
     */
    public static String restoreJsonString(String jsonStr) {
        String key = "key";
        if (jsonStr != null && !"".equals(jsonStr.trim())) {
            try {
                String sb = "{" + "\"" + key + "\":" + "\"" + jsonStr + "\"" + "}";
                return JSON.parseObject(sb).getString(key);
            } catch (JSONException exception) {
                LOGGER.error("the string is not json syntax, source={}", jsonStr);
            }
        }
        return jsonStr;
    }

}
